package org.jeecg.modules.meter.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.entity.meter.MeterFeeRule;
import org.jeecg.modules.meter.mapper.MeterFeeRuleMapper;
import org.jeecg.modules.meter.service.IMeterFeeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 仪表收费项
 * @Author: jeecg-boot
 * @Date:   2020-11-20
 * @Version: V1.0
 */
@Service
public class MeterFeeRuleServiceImpl extends ServiceImpl<MeterFeeRuleMapper, MeterFeeRule> implements IMeterFeeRuleService {

    @Autowired
    private MeterFeeRuleMapper meterFeeRuleMapper;



    @Override
    public List<MeterFeeRule> findPage(Page<MeterFeeRule> page, MeterFeeRule meterFeeRule) {
        return meterFeeRuleMapper.findPage(page, meterFeeRule);
    }

    @Override
    public MeterFeeRule getByName(String projectId, String feeName) {
        return meterFeeRuleMapper.getByName(projectId, feeName);
    }

    @Override
    public List<MeterFeeRule> getMeterFeeRule(String projectId) {
        return meterFeeRuleMapper.getMeterFeeRule(projectId);
    }

    @Override
    public MeterFeeRule queryFeeByMeterId(String meterId) {
        MeterFeeRule res = new MeterFeeRule();
        List<MeterFeeRule> meterFeeList = meterFeeRuleMapper.findByMeterId(meterId);
        if (meterFeeList == null || meterFeeList.size() == 0 ){
            return null;
        }
        BigDecimal price = BigDecimal.ZERO;
        Integer precision = 0;
        Integer round = 0;
        for (MeterFeeRule meterFeeRule:meterFeeList){
            BigDecimal subPrice =  meterFeeRule.getFeePrice() == null ? BigDecimal.ZERO: new BigDecimal(meterFeeRule.getFeePrice().toString());
            price = price.add(subPrice);
            if (meterFeeRule.getFeePrecision() > precision){
                precision = meterFeeRule.getFeePrecision();
            }
            if (meterFeeRule.getRoundMode()> round){
            round = meterFeeRule.getRoundMode();
            }
        }
        res.setFeePrice(price);
        res.setRoundMode(round);
        res.setFeePrecision(precision);
        return res;
    }

    @Override
    public List<String> queryIdByname(List<String> asList,String projectId) {
        return meterFeeRuleMapper.queryIdByname(asList,projectId);
    }

    @Override
    public String getFeeName(String itemId) {
        return meterFeeRuleMapper.getFeeName(itemId);
    }
}
